package com.itheima.service.impl;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.dao.LinkMapper;
import com.itheima.pojo.Link;
import com.itheima.pojo.LinkVO;
import com.itheima.service.LinkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class LinkServiceImpl implements LinkService {

    @Autowired
    private LinkMapper linkMapper;

    @Override
    @Transactional
    public void add(LinkVO linkVO) {
        Link link = JSON.parseObject(JSON.toJSONString(linkVO), Link.class);
        //生成一个随机的id
        String uuid = UUID.randomUUID().toString();
        String replace = uuid.replace("-", "");

        //设置随机的id
        link.setUid(replace);
        //设置创建时间
        link.setCreateTime(new Date());
        //设置更新时间
        link.setUpdateTime(new Date());

        linkMapper.insert(link);

    }

    @Override
    @Transactional
    public void delete(LinkVO linkVO) {
        //获取到前台传过来的uid
        String uid = linkVO.getUid();
        linkMapper.deleteByPrimaryKey(uid);
    }

    @Override
    @Transactional
    public void edit(LinkVO linkVO) {
        //将linkVO转换成link对象
        Link link = JSON.parseObject(JSON.toJSONString(linkVO), Link.class);
        //获取到前台传过来的uid
        String uid = linkVO.getUid();
        link.setUpdateTime(new Date());
        link.setUid(uid);
        linkMapper.updateByPrimaryKey(link);
    }

    @Override
    public PageInfo<Link> getList(LinkVO linkVO) {

        PageHelper.startPage(linkVO.getCurrentPage(),10);
        Example example = new Example(Link.class);
        example.setOrderByClause("sort DESC");
        Example.Criteria criteria = example.createCriteria();
        if(linkVO.getKeyword()!=null&&!"".equals(linkVO.getKeyword())){
            criteria.andEqualTo("title",linkVO.getKeyword());
        }
        List<Link> links = linkMapper.selectByExample(example);

        return new PageInfo<>(links);
    }

    @Override
    public void stick(LinkVO linkVO) {
        //首先查询当前表中sort字段的最大值
        int max = linkMapper.selectMaxSort();

        String uid = linkVO.getUid();
        Link link1 = new Link();
        link1.setUrl(uid);
        Link link2 = linkMapper.selectOne(link1);

        Link link = new Link();
        //把当前对象的sort设置为最大值(max+1)
        link.setSort(max+1);
        link.setUpdateTime(new Date());
        link.setStatus(1);
        link.setLinkStatus(1);
        link.setUid(uid);
        link.setTitle(link2.getTitle());
        link.setSummary(link2.getSummary());
        link.setUrl(link2.getUrl());
        link.setClickCount(link2.getClickCount());

        linkMapper.updateByPrimaryKey(link);

    }

    /**
     * 增加链接的点击数
     * @param uid
     */
    @Override
    public void addLinkCount(String uid) {
        linkMapper.addLinkCount(uid);
    }

    /**
     * 获取所有友情链接
     * @return
     */
    @Override
    public List<Link> findAll() {
        return linkMapper.selectAll();
    }
}
